/*
 * @(#)ClearFilter.java   0.2 04/02/2009
 */
package org.miao.catstorage;

import java.util.ArrayList;
import java.util.List;

/**
 * 语句清理过滤器
 * <p>用于清理语句中多余的括号、空白、逻辑操作符、"Where"关键字和"Having"关键字。</p>
 * @author Miao
 * @version 0.4
 * @since 0.2
 */
public class ClearFilter implements Filter {

    /**
     * 底层过滤器
     */
    private Filter filter;
    /**
     * 清理器
     */
    private List<Clear> clears = new ArrayList<Clear>();

    /**
     * 构造方法
     * @param filter 底层过滤器
     * @since 0.2
     */
    public ClearFilter(Filter filter) {
        this.filter = filter;
        init();
    }

    /**
     * 清理器初始化
     * @since 0.2
     */
    private void init() {
        clears.add(new BracketClear());
        clears.add(new LogicOperatorClear());
        clears.add(new HavingClear());
        clears.add(new WhereClear());
        clears.add(new BlankClear());
    }

    public String filter(String statement, List<String> parameters) {
        String filterResult = filter.filter(statement, parameters);
        for (Clear clear : clears) {
            filterResult = clear.clear(filterResult);
        }
        return filterResult;
    }

    /**
     * 设置清理器
     * @param clears 清理器
     * @since 0.2
     */
    public void setClears(List<Clear> clears) {
        this.clears = clears;
    }
}
